package com.cy.personer.biz.impl;

import com.cy.personer.bean.*;
import com.cy.personer.biz.ComboBiz;
import com.cy.personer.biz.ReportBiz;
import com.cy.personer.ceptor.OperationLog;
import com.cy.personer.mapper.ComboMapper;
import com.cy.personer.mapper.MoneyDetailsMapper;
import com.cy.personer.mapper.ReportMapper;
import com.cy.personer.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报告服务层实现类
 * @author 林少艺
 */
@Service
public class ReportBizImpl implements ReportBiz {

    @Resource
    ReportMapper reportMapper;
    @Resource
    ComboMapper comboMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    MoneyDetailsMapper moneyDetailsMapper;

    @Autowired
    Report report;
    @Autowired
    MoneyDetails moneyDetails;
    @Autowired
    ReportItem reportItem;

    /**
     * 通过套餐添加报告
     * @param user
     * @param combo
     * @param work
     * @return
     * @author 林少艺
     */
    @Override
    public String addReportCombo(User user, Combo combo, Work work) {

        //查找项目
        Combo tempCombo = comboMapper.getCombo(combo);
        //查询用户余额
        User tempUser = userMapper.getUser(user);
        if (tempUser.getUserMoney()>=tempCombo.getComboMoney()){
            //扣款
            tempUser.setUserMoney(tempUser.getUserMoney()-tempCombo.getComboMoney());
            userMapper.updataUserMoney(tempUser);
            //添加套餐使用记录
            comboMapper.addComboEmploy(combo);
            //添加资金明细
            moneyDetails.setUserId(user.getUserId());
            moneyDetails.setWorkId(work.getWorkId());
            moneyDetails.setMoney(tempCombo.getComboMoney());
            moneyDetails.setMatter(tempCombo.getComboName());
            moneyDetails.setMdTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            moneyDetailsMapper.addMoneyDetails(moneyDetails);
            //添加报告
            report.setUserId(user.getUserId());
            report.setWorkId(work.getWorkId());
            reportMapper.addReport(report);
            //查询报告编号
            report = reportMapper.getReport(report);
            //报告添加项目
            Map<String ,Object> map = new HashMap<>();
            map.put("reportId",report.getReportId());
            map.put("items",tempCombo.getItems());
            map.put("stateId",17);
            boolean state = false;
            //有项目才添加
            if (tempCombo.getItems().size()>0){
                state = reportMapper.addReportItem(map)>0;
            }
            return ""+state;
        }
        return "fundShortage";
    }

    /**
     * 查找报告项目
     * @param report
     * @return
     * @author 林少艺
     */
    @Override
    public Report getReportItem(Report report) {
        return reportMapper.getReportItem(report);
    }

    /**
     * 自选项目新增报告
     * @param user
     * @param items
     * @param work
     * @return
     * @author 林少艺
     */
    @Override
    public boolean addReportItem(User user, int[] items, Work work) {
        //添加报告
        report.setUserId(user.getUserId());
        report.setWorkId(work.getWorkId());
        reportMapper.addReport(report);
        //查询报告编号
        report = reportMapper.getReport(report);
        //报告添加项目
        Map<String ,Object> map = new HashMap<>();
        map.put("reportId",report.getReportId());
        List<Item> items1 = new ArrayList<>();
        for (int i=0;i<items.length;i++){
            Item item = new Item();
            item.setItemId(items[i]);
            items1.add(item);
        }
        map.put("items",items1);
        map.put("stateId",16);
        boolean state = false;
        //有项目才添加
        if (items.length>0){
            state = reportMapper.addReportItem(map)>0;
        }
        return state;
    }

    /**
     * 获取报告（包括全部内容）
     * @param report
     * @return
     * @author 林少艺
     */
    @Override
    public Report getReport(Report report) {
        return reportMapper.getReport(report);
    }


}
